﻿using System;
using System.Reflection;

namespace Fast.Extensions.DatabaseAccessor.EntityFrameworkCore
{
    /// <summary>
    /// 连接字符串特性
    /// </summary>
    [SkipScan, AttributeUsage(AttributeTargets.Class, Inherited = true)]
    public class ConnectionStringAttribute : Attribute
    {
        public string Name { get; }

        public ConnectionStringAttribute(string name)
        {
            Name = name;
        }
        /// <summary>
        /// 获取连接字符串名称
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string GetConnStringName<T>()
        {
            return GetConnStringName(typeof(T));
        }
        /// <summary>
        /// 获取连接字符串名称
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetConnStringName(Type type)
        {
            var nameAttribute = type.GetTypeInfo().GetCustomAttribute<ConnectionStringAttribute>();

            if (nameAttribute == null)
            {
                return type.FullName;
            }

            return nameAttribute.Name;
        }
    }
}
